<?php

/**
 *
 * @author Administrator
 */
class PopupController {
    // TODO - Insert your code here
    private $path;
    private $lan;
    
    /**
     */
    function __construct() {
        
        $this->path = "../src/module/admin/page_popup/temp/";

        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }
    
    /**
     * View popup page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewPopup(){
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/popup.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_PAGEPOPUP);
        $template->assign( ACTION, PVALUE_ACTION_VIEW_FORM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }
    
    /**
     * View form page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewForm() {
    
        $template = new XTemplate( $this->path . $this->lan . "/form.html");
    
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_PAGEPOPUP);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
    
        // get id
        $db = new DBAccess();
        $item = $db->selectOneByCause($GLOBALS['pagepopupFullColArray'], PAGEPOPUP, array(PAGEPOPUP_TYPE => $_REQUEST[PARAM_TYPE], SQL_AND, PAGEPOPUP_LANGUAGE => $this->lan));
        
        if ($item == null || count($item) == 0){
            $item = array();
            $item[PAGEIMAGE_TYPE] = $_REQUEST[PARAM_TYPE];
        }
        
        
        // set defalt flash with height
        if (!isset($item[PAGEPOPUP_FLASHWIDTH]) || empty($item[PAGEPOPUP_FLASHWIDTH])){
            $item[PAGEPOPUP_FLASHWIDTH] = $GLOBALS['flashDefaultSize'][$item[PAGEIMAGE_TYPE]][PAGEPOPUP_FLASHWIDTH];
        }
        if (!isset($item[PAGEPOPUP_FLASHHEIGHT]) || empty($item[PAGEPOPUP_FLASHHEIGHT])){
            $item[PAGEPOPUP_FLASHHEIGHT] = $GLOBALS['flashDefaultSize'][$item[PAGEIMAGE_TYPE]][PAGEPOPUP_FLASHHEIGHT];
        }
            
        // assign gird title
        $title = $GLOBALS['pagepopupTypeArray'][$this->lan][$item[PAGEIMAGE_TYPE]];
        $template->assign( FORMTITLE, $title);
    
        // assign form item
        $template->assign( FORMITEM, $item);
        $template->parse( PAGECONTENT .".".FORMITEM);
    
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }
    
    /**
     * Submit popup page
     * @return multitype:string
     */
    public function submitForm(){
        
        // reponse array
        $response_array = array();
        
        // get input
        $item = array();
        $item[PAGEPOPUP_ID] = $_REQUEST[PAGEPOPUP_ID];
        $item[PAGEPOPUP_TYPE] = $_REQUEST[PAGEPOPUP_TYPE];
        $item[PAGEPOPUP_TITLE] = $_REQUEST[PAGEPOPUP_TITLE];
        
        $item[PAGEPOPUP_FILELINK] = $_REQUEST[PAGEPOPUP_FILELINK];
        $item[PAGEPOPUP_FILETYPE] = $_REQUEST[PAGEPOPUP_FILETYPE];
        
        $item[PAGEPOPUP_IMAGETITLE] = $_REQUEST[PAGEPOPUP_IMAGETITLE];
        $item[PAGEPOPUP_IMAGEALT] = $_REQUEST[PAGEPOPUP_IMAGEALT];
        
        $item[PAGEPOPUP_FLASHWIDTH] = $_REQUEST[PAGEPOPUP_FLASHWIDTH];
        $item[PAGEPOPUP_FLASHHEIGHT] = $_REQUEST[PAGEPOPUP_FLASHHEIGHT];
        
        $item[PAGEPOPUP_LINK] = $_REQUEST[PAGEPOPUP_LINK];
        
        $item[PAGEPOPUP_UPDATETIME] = getCurrentDateTime();
        $item[PAGEPOPUP_LANGUAGE] = $this->lan;
        $item[PAGEPOPUP_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        if (empty($item[PAGEPOPUP_FILELINK]) || $item[PAGEPOPUP_FILETYPE] == "Image"){
            $item[PAGEPOPUP_FLASHWIDTH] = 0;
            $item[PAGEPOPUP_FLASHHEIGHT] = 0;
        }
        
        // update data to database
        $db = new DBAccess();
        
        if ($item[USER_ID] == 0) {
            if ($db->insertData( $item, PAGEPOPUP)) {
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_PAGEPOPUP . "&act=" . PVALUE_ACTION_VIEW_POPUP;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
            }
        } else {
            if ($db->updateData( $item, PAGEPOPUP)) {
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_PAGEPOPUP . "&act=" . PVALUE_ACTION_VIEW_POPUP;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
            }
        }
        
        // return repnose array;
        return $response_array;
        
    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>